package lupita.model.vos;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import lupita.persistence.MySQLImpl;

public class CloudTags {
	
	private  MySQLImpl mysql;
	
	public List<String> splitInputTxt(String InputTxt) {

		InputTxt = InputTxt.toLowerCase();		
		String[] completeTags = InputTxt.split(" ");
		
		List<String> optimizeTags = new ArrayList<String>();		
		
		for(int i= 0; i< completeTags.length; i++){
			if(!completeTags[i].equals("el")
					&& !completeTags[i].equals("la") //articulos
					&& !completeTags[i].equals("los")
					&& !completeTags[i].equals("las")
					&& !completeTags[i].equals("un")
					&& !completeTags[i].equals("una")
					&& !completeTags[i].equals("unos")
					&& !completeTags[i].equals("unas")
					&& !completeTags[i].equals("a") //preposiciones
					&& !completeTags[i].equals("ante")
					&& !completeTags[i].equals("bajo")
					&& !completeTags[i].equals("con")
					&& !completeTags[i].equals("contra")
					&& !completeTags[i].equals("de")
					&& !completeTags[i].equals("desde")
					&& !completeTags[i].equals("durante")
					&& !completeTags[i].equals("en")
					&& !completeTags[i].equals("entre")
					&& !completeTags[i].equals("hacia")
					&& !completeTags[i].equals("hasta")
					&& !completeTags[i].equals("mediante")
					&& !completeTags[i].equals("para")
					&& !completeTags[i].equals("por")
					&& !completeTags[i].equals("pro")
					&& !completeTags[i].equals("segun")
					&& !completeTags[i].equals("sin")
					&& !completeTags[i].equals("sobre")
					&& !completeTags[i].equals("tras")
					&& !completeTags[i].equals("via")
					&& !completeTags[i].equals("y") //conjunciones
					&& !completeTags[i].equals("e")
					&& !completeTags[i].equals("ni")
					&& !completeTags[i].equals("o")
					&& !completeTags[i].equals("bien")
					&& !completeTags[i].equals("u")
					&& !completeTags[i].equals("pero")
					&& !completeTags[i].equals("aunque")
					&& !completeTags[i].equals("sino")
					&& !completeTags[i].equals("mas")
					&& !completeTags[i].equals("sea")
					&& !completeTags[i].equals("porque")
					&& !completeTags[i].equals("pues")
					&& !completeTags[i].equals("siempre")
					&& !completeTags[i].equals("como")
					&& !completeTags[i].equals("tan")
					&& !completeTags[i].equals("cuando")
					&& !completeTags[i].equals("para")
					&& !completeTags[i].equals("")
					){
				optimizeTags.add(completeTags[i]);
			}
			
		}
		
		return optimizeTags;
	}
	
	public  boolean  updateTags(List<String> tags){
						
		try {
			mysql = new MySQLImpl();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		ResultSet result;		
		
		try{
			mysql.openConnection();
			for(String tagName: tags){
				String tagSear = "select * from tags where  tag = '" + tagName.toLowerCase() +"';";
				result=  mysql.selectSQL(tagSear);	
								
				if (result.next()){				
					//result.beforeFirst();		
					System.out.println("TABLA TAGS");		
					int frecuencia;
					String tag;						
									 						
					//while(result.is .next()) {
					//do{
						frecuencia = result.getInt("frecuencia");
						tag = result.getString("tag");			
			
						System.out.println(frecuencia+" "+tag+" ");						
						frecuencia++;
						String tagUpdate = "update tags set frecuencia = " + frecuencia + " where tag = '" +  tagName.toLowerCase() + "';"; 
						int sucess = mysql.execQuery(tagUpdate);
					//}while(result.next());
				}
				else{
					String insertTag = "insert into tags (tag,frecuencia) value ('"+ tagName.toLowerCase() +"',"+1+")";
					int success = mysql.execQuery(insertTag);
				}
			}
		
			mysql.closeConnection();
		
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return true;
	}

}
